package ph.com.gs3.formalistics.presenter.adapters;

import java.util.List;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import ph.com.gs3.formalistics.model.valueobjects.business.document.Document.DocumentStar;
import ph.com.gs3.formalistics.service.managers.ImageManager;
import ph.com.gs3.formalistics.view.valueobjects.UserActionListViewItem;
import ph.com.gs3.formalistics.view.valueobjects.UserActionListViewItem.UpdateType;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class DocumentListItemViewAdapter extends BaseAdapter {

	public static final String TAG = DocumentListItemViewAdapter.class.getSimpleName();

	// public static final int IMAGE_STARRED = R.drawable.starstrokestarred;
	// public static final int IMAGE_UNSTARRED = R.drawable.starstrokelight;

	private Context context;
	private List<UserActionListViewItem> userActionList;

	private DocumentListItemActionListener listener;

	private ImageManager imageManager;

	public DocumentListItemViewAdapter(Context context, List<UserActionListViewItem> list) {
		this.context = context;
		this.userActionList = list;

		imageManager = ImageManager.getDefaultInstance(context.getApplicationContext());

	}

	@Override
	public int getCount() {
		return userActionList.size();
	}

	@Override
	public Object getItem(int position) {
		return userActionList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return Long.parseLong(userActionList.get(position).documentDbId);
	}

	private static class DocumentListViewFragmentHolder {

		TextView tvHeader;
		TextView tvSubheader;
		TextView tvDateOnHeader;
		TextView tvContentsHeader;
		TextView tvContentsBody;
		TextView tvFooterTitle;
		TextView tvFooterContent;

		TextView tvBadge;

		Button bShowComments;
		Button bShowActions;
		Button bMarkDocumentStar;

		ImageView avatar;

		public DocumentListViewFragmentHolder(View view) {

			tvHeader = (TextView) view.findViewById(R.id.DL_tvheader);
			tvSubheader = (TextView) view.findViewById(R.id.DL_tvSubHeader);
			tvDateOnHeader = (TextView) view.findViewById(R.id.DL_tvheader_action_date);
			tvContentsHeader = (TextView) view.findViewById(R.id.DL_tvcontentsHeader);
			tvContentsBody = (TextView) view.findViewById(R.id.DL_tvcontents);
			tvFooterTitle = (TextView) view.findViewById(R.id.DL_tvfooter_title);
			tvFooterContent = (TextView) view.findViewById(R.id.DL_tvfooter_content);

			tvBadge = (TextView) view.findViewById(R.id.DL_tvbadge);

			bShowComments = (Button) view.findViewById(R.id.DL_bComment);
			bShowActions = (Button) view.findViewById(R.id.DL_bActions);
			bMarkDocumentStar = (Button) view.findViewById(R.id.DL_bStar);

			avatar = (ImageView) view.findViewById(R.id.DL_ivAvatar);

		}

	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		Animation fadeInAnimation;
		fadeInAnimation = AnimationUtils.loadAnimation(context, R.animator.fade_in);

		View row = convertView;
		DocumentListViewFragmentHolder holder = null;

		final UserActionListViewItem userActionListViewItem = userActionList.get(position);

		if (row == null) {

			LayoutInflater inflater = (LayoutInflater) context
			        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.view_document_list_item, parent, false);
			holder = new DocumentListViewFragmentHolder(row);
			row.setTag(holder);
			holder.bShowActions.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (listener != null) {
						listener.onClickedActions(userActionListViewItem);
					}
				}
			});

			holder.bShowComments.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (listener != null) {
						listener.onClickedComments(userActionListViewItem);
					}
				}
			});

			holder.bMarkDocumentStar.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {

					FLLogger.d(TAG, "Clicked on star button of document "
					        + userActionListViewItem.documentWebId + " "
					        + userActionListViewItem.header + " "
					        + userActionListViewItem.contentsHeader);

					if (((UserActionListViewItem) getItem(position)).isStarred) {
						listener.onDocumentStarMarked(userActionListViewItem,
						        DocumentStar.UNSTARRED);
						v.setBackgroundResource(R.drawable.starstrokebordered);

						FLLogger.d(TAG, "Mark as unstarred");
					} else {
						listener.onDocumentStarMarked(userActionListViewItem, DocumentStar.STARRED);
						v.setBackgroundResource(R.drawable.starstrokeborderedpressed);

						FLLogger.d(TAG, "Mark as starred");
					}

				}
			});

		} else {
			holder = (DocumentListViewFragmentHolder) row.getTag();
			// row.setAnimation(fadeInAnimation);
		}

		holder.tvHeader.setText(userActionListViewItem.header);
		holder.tvSubheader.setText(userActionListViewItem.subheader);
		holder.tvDateOnHeader.setText(userActionListViewItem.date);
		holder.tvContentsHeader.setText(userActionListViewItem.contentsHeader);
		holder.tvContentsBody.setText(userActionListViewItem.contentsBody);
		holder.tvFooterTitle.setText(userActionListViewItem.footerHeader);
		holder.tvFooterContent.setText(userActionListViewItem.footerBody);

		if (userActionListViewItem.isStarred) {
			holder.bMarkDocumentStar.setBackgroundResource(R.drawable.starstrokeborderedpressed);
		} else {
			holder.bMarkDocumentStar.setBackgroundResource(R.drawable.starstrokebordered);
		}

		// button visibility
		// @formatter:off
		holder.bShowActions.setVisibility(
				userActionListViewItem.showActionsButton 	? View.VISIBLE : View.GONE);
		holder.bMarkDocumentStar.setVisibility(
				userActionListViewItem.showStarButton 		? View.VISIBLE : View.GONE);
		holder.bShowComments.setVisibility(
				userActionListViewItem.showCommentButton 	? View.VISIBLE : View.GONE);
		// @formatter:on

		// default update type/current
		holder.tvBadge.setVisibility(View.GONE);

		if (userActionListViewItem.updateType == UpdateType.NEW) {
			holder.tvBadge.setText("New");
			holder.tvBadge.setAnimation(fadeInAnimation);
			// row.setAnimation(fadeInAnimation);

			holder.tvBadge.setVisibility(View.VISIBLE);
		} else if (userActionListViewItem.updateType == UpdateType.UPDATE) {
			holder.tvBadge.setText("Updated");
			holder.tvBadge.setAnimation(fadeInAnimation);
			// row.setAnimation(fadeInAnimation);

			holder.tvBadge.setVisibility(View.VISIBLE);
		}

		imageManager.requestUserImage(userActionListViewItem.authorWebId,
		        userActionListViewItem.server, holder.avatar);

		return row;
	}

	public void setActionListener(DocumentListItemActionListener listener) {
		this.listener = listener;
	}

	public static interface DocumentListItemActionListener {

		public void onClickedActions(UserActionListViewItem userActionListViewItem);

		public void onDocumentStarMarked(UserActionListViewItem userActionListViewItem,
		        DocumentStar newDocumentStar);

		public void onClickedComments(UserActionListViewItem userActionListViewItem);

	}

}
